#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "hashFns.c"

#define FILTER_SIZE 100
#define NUM_HASHES 7
#define WORD_BUF_SIZE 32
int READ_SIZE=40;


typedef struct node{
	int doc;
	int pos;
	struct node *next;
} node;

node* list[FILTER_SIZE];
unsigned int filter[FILTER_SIZE];

void load_words(char *, int);
void insert_word(char *,int, int);
//int in_dict(unsigned char[], char *);
void get_hashes(unsigned int[], char *);



int main(int argc, char *argv[])
{	
	int i;
        FILE *f;
        if (argc < 3)
		{printf("not enough input\n"); return 0;}
	for(i=0;i<FILTER_SIZE;i++)
		{ 
                list[i]=(node *)malloc(sizeof(node)*1);
                list[i]->next=NULL;
		//printf("%p ",list[i]);
		}

	for (i = 0; i < FILTER_SIZE; i++)
		filter[i] = 0;
	//printf("going to load words \n");

// modify read-size to smaller length
	int upperf1, upperf2, smaller;
	FILE *f1 = fopen(argv[1], "r");
	fseek(f1,0,SEEK_END);
	upperf1= ftell(f1);
	FILE *f2 = fopen(argv[2], "r");
	fseek(f2,0,SEEK_END);
	upperf2= ftell(f2);
	smaller=(upperf1<upperf2)?upperf1:upperf2;
	READ_SIZE=(READ_SIZE>smaller)?smaller:READ_SIZE;
	//printf("%d\n",READ_SIZE);
	fclose(f1);
	fclose(f2);

	load_words(argv[1],1);
        load_words(argv[2],2);


for(i=0;i<FILTER_SIZE;i++)
{
	node *tmp=list[i]->next;
	if(tmp!=NULL)
	{while(tmp->next!=NULL)
		{
		printf("%d,%d ", tmp->doc, tmp->pos);
		tmp=tmp->next;
		}
	}
}
// string matching code

return 0;

}


void load_words(char *filename,int doc_no)
{			
	FILE *f = fopen(filename, "r");
	int c,k,size,upper;
        size=0;
        char read_file[READ_SIZE];
	if (!f)
		{printf("unable to open file \"%s\"\n", filename); return;}
          if((fseek(f,0,SEEK_END))==-1){
          printf("ERROR: error in reading file \n");
          return;
          }
         upper=ftell(f);
        

         while(size<=(upper-(READ_SIZE-1)))
       {
         if((fseek(f,size,SEEK_SET))==-1)
	  {
          printf("ERROR: error in reading file \n");
          return;
          }
         
          for(k=0;k<(READ_SIZE-1);k++)
          { read_file[k]=fgetc(f);
         // fscanf(f,"%s",read_file);
          }
           read_file[READ_SIZE-1]='\0';
          //printf("%s\n",read_file);
         insert_word(read_file,doc_no, size);
 	  size=size+1;
        }

   fclose(f);
}

void insert_word( char *str, int doc_no, int size)
{
	unsigned int hash[NUM_HASHES],d;
	int i;
	//printf("in insert word fn \n");
	get_hashes(hash, str);
	//printf("returned from get hashes\n");
	for (i = 0; i < NUM_HASHES; i++) 
	{
                 hash[i]=hash[i]%97;
                 //printf("%u\n for 97\n",hash[i]);
		 filter[hash[i]]++;
		 //printf("%d ",i);
		node *temp=(node *)malloc(sizeof(node));
		temp->doc=doc_no; 	
		temp->pos=size;
		temp->next=NULL;
		
		if(list[hash[i]]->next==NULL)
		{
                
		list[hash[i]]->next=temp;
		 //int z;scanf("%d",&z);
		}
		
		else
		{node *temp1=list[hash[i]]->next;
		while(temp1->next !=NULL)
			{
			 temp1=temp1->next;
			}
		 temp1->next=temp;
		}
	}
/*
          for(i=0;i<FILTER_SIZE;i++)
          { 
             printf("%d ",list[i]->doc);       
          }
*/
}

void get_hashes(unsigned int hash[], char *in)
{       
        int i;
	unsigned char *str = (unsigned char *)in;
	//printf("%c\n",*str);
	int pos = strlen(in);
	hash[0] = RSHash  (str, pos);
	hash[1] = DJBHash (str, pos);
	hash[2] = FNVHash (str, pos);
	hash[3] = JSHash  (str, pos);
	hash[4] = PJWHash (str, pos);
	hash[5] = SDBMHash(str, pos);
	hash[6] = DEKHash (str, pos);
/*
     for(i=0;i<7;i++)
       {
	printf("the value of hash[%d] is %u\n",i,hash[i]);        
	printf("%u\n",hash[i]);
        }
 */    

}


